package com.test.nyamecredit.tool

import android.content.Context
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.text.style.StyleSpan
import android.view.View
import android.widget.TextView
import androidx.annotation.ColorRes
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import java.util.regex.Pattern

/**
 * 文本复杂处理工具
 */
object TxtTAType {


    /**
     * 文本变形工具
     */
    class TxtTransformBType(private val ctxCType: Context, parentCType: String) {

        /** 最终生成字段 */
        private val txtFinalCType = SpannableString(parentCType)

        /** 用于比较的父字段 */
        private val txtParentCType = parentCType.lowercase()

        /** 用于比较的目标字段 */
        private var txtTargetCType = ""

        /** 文字字体 */
        private var txtStyleCType = -1

        /** 文字大小 */
        private var txtSizeCType = -1f

        /** 文字颜色 */
        private var txtColorResCType = -1

        /** 文字点击事件 */
        private var txtClickCType: TxtClickBType? = null


        /** 设置 目标字段（可反复设置来支持多段文字） */
        fun setTargetBType(txtCType: String): TxtTransformBType {
            txtTargetCType = txtCType.lowercase()
            return this
        }

        /**
         * 设置文字字体
         *
         * @param styleCType Typeface.XXX
         */
        fun setStyleBType(styleCType: Int): TxtTransformBType {
            txtStyleCType = styleCType
            return this
        }

        /** 设置文字大小 */
        fun setSizeBType(sizeCType: Float): TxtTransformBType {
            txtSizeCType = sizeCType
            return this
        }

        /** 设置文字颜色 */
        fun setColorBType(@ColorRes colorCType: Int): TxtTransformBType {
            txtColorResCType = colorCType
            return this
        }

        /** 设置点击事件 */
        fun setClickBType(clickCType: TxtClickBType): TxtTransformBType {
            txtClickCType = clickCType
            return this
        }

        /** 匹配文字 */
        suspend fun matchBType(): TxtTransformBType {
            withContext(Dispatchers.Default) {
                Pattern.compile(txtTargetCType).matcher(txtParentCType).let { match ->
                    while (match.find()) {
                        val startPosCType = match.start()
                        val endPosCType = match.end()

                        // 设置大小
//                        if (txtSizeCType != -1f)
//                            txtFinalCType.setSpan(
//                                AbsoluteSizeSpan(txtSizeCType.spToPxBType(ctxCType).toInt()),
//                                startPosCType,
//                                endPosCType,
//                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
//                            )
                        // 设置颜色
                        if (txtColorResCType != -1)
                            txtFinalCType.setSpan(
                                ForegroundColorSpan(txtColorResCType),
                                startPosCType,
                                endPosCType,
                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                            )
                        // 设置字体
                        if (txtStyleCType != -1)
                            txtFinalCType.setSpan(
                                StyleSpan(txtStyleCType),
                                startPosCType,
                                endPosCType,
                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                            )
                        // 设置点击事件
                        txtClickCType?.let {
                            txtFinalCType.setSpan(
                                it,
                                startPosCType,
                                endPosCType,
                                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
                            )
                        }
                    }
                }
            }
            return this
        }

        /** 把文字填充到TextView */
        fun updateBType(tvCType: TextView) {
            txtClickCType?.let {
                tvCType.movementMethod = LinkMovementMethod.getInstance()
            }
            tvCType.text = txtFinalCType
        }

        /**
         * 自定义文字段点击监听
         */
        abstract class TxtClickBType(private val showUnderlineCType: Boolean) : ClickableSpan() {
            override fun onClick(widget: View) {
                clickEventBType()
            }

            override fun updateDrawState(ds: TextPaint) {
                ds.isUnderlineText = showUnderlineCType
            }

            abstract fun clickEventBType()
        }
    }
}